//
//  DestinationCollectionViewController.m
//  xiaowanTravel
//
//  Created by wyzcWLB on 16/9/30.
//  Copyright © 2016年 wyzcWLB. All rights reserved.
//

#import "DestinationCollectionViewController.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import "hotCountryCell.h"
#import "Data.h"
#import <UIImageView+WebCache.h>
#import "HeadCollectionReusableView.h"
#import "DetailViewController.h"
#import <MJRefresh.h>

@interface DestinationCollectionViewController ()
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *hotCityArray;
@end

@implementation DestinationCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topnav"]forBarMetrics:UIBarMetricsDefault];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView  registerNib:[UINib nibWithNibName:@"hotCountryCell" bundle:nil] forCellWithReuseIdentifier:@"hotCountryCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HeadCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"HeadCollectionReusableView"];
    
    [self loadData];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)hotCityArray{
    
    if (!_hotCityArray) {
        _hotCityArray = [NSMutableArray array];
    }
    return _hotCityArray;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(void)loadData{
    [self.dataArray removeAllObjects];
    [self.collectionView.mj_header beginRefreshing];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *url = @"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=40.03623447101837&lon=116.363814398621&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=7B6FA080-F9FB-44C1-B932-401CD69CD5D2&track_os=ios%25209.1&v=1";
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        Data *desModel=[[Data alloc]mj_setKeyValues:dic];
        NSMutableArray *array = [NSMutableArray new];
        array = desModel.data;
        for (DataModel *model in array)
        {
            [_dataArray addObject:model];
        }
        
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网路错误"];
    }];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    DataModel *model = _dataArray[section];
    return model.hot_country.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    hotCountryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotCountryCell" forIndexPath:indexPath];
    
    DataModel *model = _dataArray[indexPath.section];
    HotCountryModel *hotModel = model.hot_country[indexPath.row];
    
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:hotModel.photo]];
    cell.photo.layer.cornerRadius = 8;
    cell.photo.layer.masksToBounds = YES;
    cell.cnname.text = hotModel.cnname;
    cell.enname.text = hotModel.enname;
    cell.count.text = [NSString stringWithFormat:@"%ld",hotModel.count];
    
    return cell;
    
}
#pragma mark 点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DataModel *model = _dataArray[indexPath.section];
    HotCountryModel *hotModel = model.hot_country[indexPath.row];
    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.DetialID = hotModel.ID;
    //[self.navigationController pushViewController:detail animated:YES];
    [self presentViewController:detail animated:YES completion:nil];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section % 2!=0) {
        return CGSizeMake((Screen_Width-4)/2, 260*(Screen_Height/667.0));
    }else{
        if (indexPath.row ==0) {
            return CGSizeMake(Screen_Width, 200*(Screen_Height/667.0));
        }else if (indexPath.row == 5)
        {
            return CGSizeMake(Screen_Width, 200*(Screen_Height/667.0));
        }
        else
        {
            return CGSizeMake((Screen_Width-4)/2, 180*(Screen_Height/667.0));
        }
    }
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}
//设置最小列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}
#pragma mark 设置头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        HeadCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadCollectionReusableView" forIndexPath:indexPath];
        
        DataModel *model = _dataArray[indexPath.section];
        head.headTitle.text = model.cnname;
        return head;
    }
    return reusableview;
}
#pragma mark 设置组头视图尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(Screen_Width-80, 30);
}


@end
